<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>采购合同模板</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }

        .layuimini-form .layui-form-label {
            width: 120px;
        }
        
        /* 添加整齐的表单布局样式 */
        .layui-form-item .layui-inline {
            margin-right: 0;
            margin-bottom: 10px;
        }
        
        .layui-form-item .layui-input-inline {
            width: 220px;
            margin-right: 10px;
        }
        
        /* 明细字段区域样式调整 */
        .detail-section {
            margin-top: 20px;
            padding: 15px;
            border: 1px solid #e6e6e6;
            border-radius: 4px;
        }
        
        .detail-section legend {
            width: auto;
            margin-bottom: 10px;
            font-size: 16px;
            font-weight: bold;
        }
        
        /* 合同格式样式 */
        .contract-format {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            font-family: "Microsoft YaHei", sans-serif;
        }
        
        .contract-header {
            text-align: center;
            margin-bottom: 30px;
        }
        
        .contract-title {
            font-size: 24px;
            font-weight: bold;
            margin-bottom: 10px;
        }
        
        .contract-info {
            display: flex;
            justify-content: space-between;
            margin-bottom: 20px;
        }
        
        .contract-info-item {
            flex: 1;
        }
        
        .contract-table {
            width: 100%;
            border-collapse: collapse;
            margin: 20px 0;
        }
        
        .contract-table th,
        .contract-table td {
            border: 1px solid #000;
            padding: 8px;
            text-align: center;
        }
        
        .contract-table th {
            background-color: #f2f2f2;
        }
        
        .contract-signature {
            display: flex;
            justify-content: space-between;
            margin-top: 50px;
        }
        
        .signature-item {
            flex: 1;
        }
        
        .text-center {
            text-align: center;
        }
        
        .text-right {
            text-align: right;
        }
        
        /* 禁用输入框样式 */
        input:disabled, select:disabled, textarea:disabled {
            background-color: #f5f5f5;
            color: #333;
            cursor: not-allowed;
        }
        
        /* 合同条款区域样式 */
        .contract-clauses {
            margin: 20px 0;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        
        .clause-item {
            margin-bottom: 15px;
        }
        
        .clause-title {
            font-weight: bold;
            margin-bottom: 5px;
        }
        
        .clause-content {
            line-height: 1.6;
            text-indent: 2em;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <div class="contract-format">
            <!-- 合同头部 -->
            <div class="contract-header">
                <div class="contract-title">采购合同</div>
            </div>
            
            <!-- 合同基本信息 -->
            <div class="contract-info">
                <div class="contract-info-item">
                    <label>合同号：</label>
                    <input type="text" name="contractNo" id="contractNo" disabled value="" autocomplete="off" class="layui-input">
                </div>
                <div class="contract-info-item">
                    <label>签约日期：</label>
                    <input type="text" name="signDate" id="signDate" disabled value="" autocomplete="off" class="layui-input">
                </div>
                <div class="contract-info-item">
                    <label>供应商：</label>
                    <input type="text" name="supName" id="supName" disabled value="" autocomplete="off" class="layui-input">
                </div>
            </div>
            
            <!-- 合同详细信息 -->
            <fieldset class="table-search-fieldset">
                <legend>采购合同信息</legend>
                <div style="margin: 3px 3px 3px 3px">
                    <form class="layui-form layuimini-form" action="">
                        <input type="text" name="contractInnerId" id="contractInnerId" style="display: none" class="layui-input">
                        <input type="text" name="purchaseOrderId" id="purchaseOrderId" style="display: none" class="layui-input">
                        
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">合同号</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="contractNoDetail" id="contractNoDetail" value="" autocomplete="off" class="layui-input" disabled>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">关联订单号</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="relatedOrderNo" id="relatedOrderNo" value="" autocomplete="off" class="layui-input" disabled>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">供应商</label>
                                <div class="layui-input-inline">
                                    <select name="supId" id="supId" lay-search="" disabled>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">签约日期</label>
                                <div class="layui-input-inline">
                                    <input readonly="readonly" type="text" class="layui-input" name="signDateDetail" id="signDateDetail" disabled>
                                </div>
                            </div>
                        </div>
                        
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">有效起始日期</label>
                                <div class="layui-input-inline">
                                    <input readonly="readonly" type="text" class="layui-input" name="validStartDate" id="validStartDate" disabled>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">有效结束日期</label>
                                <div class="layui-input-inline">
                                    <input readonly="readonly" type="text" class="layui-input" name="validEndDate" id="validEndDate" disabled>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">交期计划方式</label>
                                <div class="layui-input-inline">
                                    <select name="deliveryPlanType" id="deliveryPlanType" disabled>
                                        <option value="">请选择</option>
                                        <option value="FIXED_DATE">固定日期</option>
                                        <option value="BY_ORDER_RATIO">按订单比例</option>
                                        <option value="INSTALLMENT">分期</option>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">预计交货日期</label>
                                <div class="layui-input-inline">
                                    <input readonly="readonly" type="text" class="layui-input" name="expectedDeliveryDate" id="expectedDeliveryDate" disabled>
                                </div>
                            </div>
                        </div>
                        
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">提货期限开始</label>
                                <div class="layui-input-inline">
                                    <input readonly="readonly" type="text" class="layui-input" name="pickupPeriodStart" id="pickupPeriodStart" disabled>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">提货期限结束</label>
                                <div class="layui-input-inline">
                                    <input readonly="readonly" type="text" class="layui-input" name="pickupPeriodEnd" id="pickupPeriodEnd" disabled>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">付款期限类型</label>
                                <div class="layui-input-inline">
                                    <select name="paymentTermsType" id="paymentTermsType" disabled>
                                        <option value="">请选择</option>
                                        <option value="SIGN_AFTER_N">签约后N天</option>
                                        <option value="DELIVERY_AFTER_N">交货后N天</option>
                                        <option value="MONTH_END_N">月末后N天</option>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">付款期限天数</label>
                                <div class="layui-input-inline">
                                    <input type="number" name="paymentTermsDays" id="paymentTermsDays" value="" autocomplete="off" class="layui-input" disabled>
                                </div>
                            </div>
                        </div>
                        
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">增值税税率</label>
                                <div class="layui-input-inline">
                                    <input type="number" name="taxRate" id="taxRate" value="" autocomplete="off" class="layui-input" disabled>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">合同总金额</label>
                                <div class="layui-input-inline">
                                    <input type="number" name="totalAmount" id="totalAmount" disabled value="" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">不含税总金额</label>
                                <div class="layui-input-inline">
                                    <input type="number" name="totalTaxFreeAmount" id="totalTaxFreeAmount" disabled value="" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">增值税金额</label>
                                <div class="layui-input-inline">
                                    <input type="number" name="taxAmount" id="taxAmount" disabled value="" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                        </div>
                        
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">合同状态</label>
                                <div class="layui-input-inline">
                                    <select name="contractStatus" id="contractStatus" disabled>
                                        <option value="10">准备</option>
                                        <option value="11">驳回</option>
                                        <option value="15">修改</option>
                                        <option value="30">待审</option>
                                        <option value="70">执行</option>
                                        <option value="80">完结</option>
                                        <option value="90">终止</option>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">业务来源</label>
                                <div class="layui-input-inline">
                                    <select name="bizSource" id="bizSource" disabled>
                                        <option value="">请选择</option>
                                        <option value="INTERNAL">内部</option>
                                        <option value="EXTERNAL">外部</option>
                                        <option value="FRAMEWORK">框架协议</option>
                                        <option value="OTHER">其他</option>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">来源单据号</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="sourceDocNo" id="sourceDocNo" value="" autocomplete="off" class="layui-input" disabled>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">版本号</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="docVersion" id="docVersion" value="" autocomplete="off" class="layui-input" disabled>
                                </div>
                            </div>
                        </div>
                        
                        <div class="layui-form-item layui-form-text">
                            <label class="layui-form-label">交期条款</label>
                            <div class="layui-input-block">
                                <textarea name="deliveryTerms" id="deliveryTerms" value="" autocomplete="off" class="layui-textarea" disabled></textarea>
                            </div>
                        </div>
                        
                        <div class="layui-form-item layui-form-text">
                            <label class="layui-form-label">业务来源说明</label>
                            <div class="layui-input-block">
                                <textarea name="bizSourceDesc" id="bizSourceDesc" value="" autocomplete="off" class="layui-textarea" disabled></textarea>
                            </div>
                        </div>
                    </form>
                </div>
            </fieldset>
            
            <!-- 合同条款区域 -->
            <div class="contract-clauses">
                <h3>合同条款</h3>
                
                <div class="clause-item">
                    <div class="clause-title">1. 知识产权保护条款</div>
                    <div class="clause-content">
                        <textarea name="intellectualPropertyClause" id="intellectualPropertyClause" 
                                  class="layui-textarea" style="width: 100%; min-height: 100px;" disabled></textarea>
                    </div>
                </div>
                
                <div class="clause-item">
                    <div class="clause-title">2. 保密义务条款</div>
                    <div class="clause-content">
                        <textarea name="confidentialityClause" id="confidentialityClause" 
                                  class="layui-textarea" style="width: 100%; min-height: 100px;" disabled></textarea>
                    </div>
                </div>
                
                <div class="clause-item">
                    <div class="clause-title">3. 违约责任条款</div>
                    <div class="clause-content">
                        <textarea name="liabilityClause" id="liabilityClause" 
                                  class="layui-textarea" style="width: 100%; min-height: 100px;" disabled></textarea>
                    </div>
                </div>
                
                <div class="clause-item">
                    <div class="clause-title">4. 其他条款</div>
                    <div class="clause-content">
                        <textarea name="otherClauses" id="otherClauses" 
                                  class="layui-textarea" style="width: 100%; min-height: 100px;" disabled></textarea>
                    </div>
                </div>
            </div>
            
            <!-- 合同明细表格 -->
            <fieldset class="table-search-fieldset">
                <legend>合同明细</legend>
                <table class="layui-hide" id="contractDetailTable" lay-filter="contractDetailFilter"></table>
            </fieldset>
            
            <!-- 操作按钮 -->
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="save-btn">保存合同</button>
                    <button class="layui-btn layui-btn-normal" lay-event="preview">预览合同</button>
                    <button class="layui-btn layui-btn-primary" lay-event="print">打印合同</button>
                    <button type="button" class="layui-btn layui-btn-primary" onclick="parent.layer.closeAll();">关闭</button>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="/static/lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="/static/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/static/lib/jq-module/jquery.particleground.min.js" charset="utf-8"></script>
<script src="/static/js/common.js"></script>
<script>
    var lay, form, table, laydate;
    layui.use(['form', 'table', 'laydate'], function () {
        lay = layui.jquery;
        form = layui.form;
        table = layui.table;
        laydate = layui.laydate;

        // 初始化合同明细表格
        table.render({
            elem: '#contractDetailTable',
            url: '/purchaseContract/getContractDetails',
            method: "post",
            contentType: "application/json; charset=utf-8",
            cols: [[
                {field: 'prodId', width: 100, title: '产品ID'},
                {field: 'productName', width: 150, title: '产品名称'},
                {field: 'productSpec', width: 150, title: '产品规格'},
                {field: 'installmentNo', width: 100, title: '分期批次'},
                {field: 'installmentExpectedDate', width: 120, title: '预计交货日期', 
                    templet: "<div>{{layui.util.toDateString(d.installmentExpectedDate, 'yyyy-MM-dd')}}</div>"
                },
                {field: 'installmentQuantity', width: 100, title: '本批次数量'},
                {field: 'installmentRatio', width: 100, title: '占比(%)'},
                {field: 'quantity', width: 80, title: '总数量'},
                {field: 'unit', width: 80, title: '单位'},
                {field: 'unitPrice', width: 100, title: '单价'},
                {field: 'subtotalAmount', width: 120, title: '分项金额'},
                {field: 'taxFreeUnitPrice', width: 100, title: '不含税单价'},
                {field: 'taxFreeAmount', width: 120, title: '不含税金额'},
                {field: 'taxRate', width: 80, title: '税率'},
                {field: 'taxAmount', width: 100, title: '增值税金额'},
                {field: 'remark', width: 150, title: '备注'}
            ]],
            done: function (res, curr, count) {
                // 表格渲染完成后的处理
            },
            limits: [10, 20, 50, 100],
            limit: 10,
            page: false
        });

        // 监听保存按钮
        form.on('submit(save-btn)', function (data) {
            saveContract(data.field);
            return false;
        });

        // 监听其他按钮
        form.on('submit', function(data) {
            if (data.elem.getAttribute('lay-event') === 'preview') {
                previewContract();
            } else if (data.elem.getAttribute('lay-event') === 'print') {
                printContract();
            }
        });

        // 检查URL参数，加载订单数据
        var urlParams = new URLSearchParams(window.location.search);
        var contractData = urlParams.get('contractData');
        var contractId = urlParams.get('contractId');
        var orderId = urlParams.get('orderId');
        var isShow = urlParams.get('isShow');
        
        if (contractData && isShow === 'true') {
            // 显示模式：直接使用传递的合同数据
            var decodedData = JSON.parse(decodeURIComponent(contractData));
            if (decodedData.contract && decodedData.details) {
                // 包含合同和明细数据的完整数据
                fillContractForm(decodedData.contract);
                fillContractDetailsTable(decodedData.details);
            } else {
                // 只有合同数据，需要加载明细（已废弃，使用数据传递方式）
                fillContractForm(decodedData);
                console.log('需要明细数据，请使用数据传递方式');
                // loadContractDetails(decodedData.contractInnerId);
            }
        } else if (contractId && isShow === 'true') {
            // 显示模式：通过API加载合同数据
            loadContractData(contractId);
        } else if (orderId) {
            // 编辑模式：加载订单数据
            loadOrderData(orderId);
        }
    });

    // 加载合同数据并填充到合同模板
    function loadContractData(contractId) {
        $.xAjax('/purchaseContract/' + contractId, 'GET', '', {
            onSuccess: function(result) {
                if (result.code == 0) {
                    var contractData = result.data;
                    fillContractForm(contractData);
                    // 加载合同明细
                    loadContractDetails(contractId);
                } else {
                    $.common.showErrorMessage(result.message);
                }
            },
            onError: function(xhr, status, error) {
                $.common.showErrorMessage('加载合同数据失败：' + error);
            }
        });
    }

    // 填充合同表单数据
    function fillContractForm(contractData) {
        // 填充合同基本信息
        $('#contractInnerId').val(contractData.contractInnerId);
        $('#contractNo').val(contractData.contractNo);
        $('#relatedOrderNo').val(contractData.relatedOrderNo);
        $('#lastBizDocNo').val(contractData.lastBizDocNo);
        $('#bizDocCode').val(contractData.bizDocCode);
        $('#signDate').val(layui.util.toDateString(contractData.signDate, 'yyyy-MM-dd'));
        $('#validStartDate').val(layui.util.toDateString(contractData.validStartDate, 'yyyy-MM-dd'));
        $('#validEndDate').val(layui.util.toDateString(contractData.validEndDate, 'yyyy-MM-dd'));
        $('#signLocation').val(contractData.signLocation || '');
        $('#deliveryPlanType').val(contractData.deliveryPlanType);
        $('#deliveryTerms').val(contractData.deliveryTerms || '');
        $('#expectedDeliveryDate').val(layui.util.toDateString(contractData.expectedDeliveryDate, 'yyyy-MM-dd'));
        $('#pickupPeriodStart').val(layui.util.toDateString(contractData.pickupPeriodStart, 'yyyy-MM-dd'));
        $('#pickupPeriodEnd').val(layui.util.toDateString(contractData.pickupPeriodEnd, 'yyyy-MM-dd'));
        $('#paymentTermsType').val(contractData.paymentTermsType);
        $('#paymentTermsDays').val(contractData.paymentTermsDays);
        $('#docVersion').val(contractData.docVersion);
        $('#versionEffectiveTime').val(layui.util.toDateString(contractData.versionEffectiveTime, 'yyyy-MM-dd HH:mm:ss'));
        $('#originalEffectiveDate').val(layui.util.toDateString(contractData.originalEffectiveDate, 'yyyy-MM-dd'));
        $('#bizSource').val(contractData.bizSource);
        $('#bizSourceDesc').val(contractData.bizSourceDesc || '');
        $('#sourceDocNo').val(contractData.sourceDocNo || '');
        
        // 填充合同条款
        $('#intellectualPropertyClause').val(contractData.intellectualPropertyClause || '');
        $('#confidentialityClause').val(contractData.confidentialityClause || '');
        $('#liabilityClause').val(contractData.liabilityClause || '');
        $('#otherClauses').val(contractData.otherClauses || '');
        
        // 填充金额信息
        $('#quantity').val(contractData.quantity);
        $('#unit').val(contractData.unit);
        $('#unitPrice').val(contractData.unitPrice);
        $('#isTempPrice').val(contractData.isTempPrice);
        $('#taxFreeUnitPrice').val(contractData.taxFreeUnitPrice);
        $('#taxFreeAmount').val(contractData.taxFreeAmount);
        $('#taxRate').val(contractData.taxRate);
        $('#taxAmount').val(contractData.taxAmount);
        $('#totalAmount').val(contractData.totalAmount);
        $('#amountRemark').val(contractData.amountRemark || '');
        
        // 填充状态信息
        $('#contractStatus').val(contractData.contractStatus);
        $('#redFlag').val(contractData.redFlag);
        
        form.render();
    }

    // 加载合同明细数据
    function loadContractDetails(contractId) {
        $.xAjax('/purchaseContract/details/' + contractId, 'GET', '', {
            onSuccess: function(result) {
                if (result.code == 0) {
                    var details = result.data;
                    fillContractDetailsTable(details);
                } else {
                    $.common.showErrorMessage(result.message);
                }
            },
            onError: function(xhr, status, error) {
                $.common.showErrorMessage('加载合同明细失败：' + error);
            }
        });
    }

    // 加载订单数据并填充到合同模板
    function loadOrderData(orderId) {
        $.xAjax('/purchaseOrder/getById/' + orderId, 'GET', '', {
            onSuccess: function(result) {
                if (result.code == 0) {
                    var orderData = result.data;
                    fillContractTemplate(orderData);
                    // 同时加载订单明细数据（已废弃，使用数据传递方式）
                    console.log('需要订单明细数据，请使用数据传递方式');
                    // loadOrderDetails(orderId);
                } else {
                    $.common.showErrorMessage(result.message);
                }
            }
        });
    }

    // 填充合同模板数据
    function fillContractTemplate(orderData) {
        // 填充基本信息
        $('#contractNo').val('HT' + orderData.orderNo);
        $('#contractNoDetail').val('HT' + orderData.orderNo);
        $('#relatedOrderNo').val(orderData.orderNo);
        $('#signDate').val(layui.util.toDateString(new Date(), 'yyyy-MM-dd'));
        $('#signDateDetail').val(layui.util.toDateString(new Date(), 'yyyy-MM-dd'));
        $('#supName').val(orderData.supplierName);
        $('#validStartDate').val(layui.util.toDateString(orderData.validStartDate, 'yyyy-MM-dd'));
        $('#validEndDate').val(layui.util.toDateString(orderData.validEndDate, 'yyyy-MM-dd'));
        $('#deliveryPlanType').val(orderData.deliveryPlanType);
        $('#expectedDeliveryDate').val(layui.util.toDateString(orderData.expectedDeliveryDate, 'yyyy-MM-dd'));
        $('#pickupPeriodStart').val(layui.util.toDateString(orderData.pickupPeriodStart, 'yyyy-MM-dd'));
        $('#pickupPeriodEnd').val(layui.util.toDateString(orderData.pickupPeriodEnd, 'yyyy-MM-dd'));
        $('#taxRate').val(orderData.taxRate || 13.00);
        $('#totalAmount').val(orderData.totalAmount);
        $('#totalTaxFreeAmount').val(orderData.totalTaxFreeAmount);
        $('#taxAmount').val(orderData.taxAmount);
        $('#contractStatus').val('10'); // 准备状态
        $('#bizSource').val(orderData.bizSource || 'INTERNAL');
        $('#sourceDocNo').val(orderData.sourceDocNo || '');
        $('#docVersion').val('V1');
        $('#deliveryTerms').val(orderData.deliveryTerms || '');
        $('#bizSourceDesc').val(orderData.bizSourceDesc || '');
        $('#purchaseOrderId').val(orderData.purchaseOrderId);
        
        // 填充默认合同条款
        $('#intellectualPropertyClause').val('双方在履行本合同过程中产生的知识产权归各自所有，未经对方书面同意，不得使用对方的商标、专利、技术秘密等知识产权。');
        $('#confidentialityClause').val('双方应对在履行本合同过程中知悉的对方商业秘密和技术秘密承担保密义务，未经对方书面同意，不得向第三方披露。');
        $('#liabilityClause').val('任何一方违反本合同约定的，应承担违约责任，赔偿对方因此遭受的损失。');
        $('#otherClauses').val('本合同未尽事宜，双方可另行协商签订补充协议，补充协议与本合同具有同等法律效力。');
        
        form.render();
        
        // 加载合同明细（已废弃，使用数据传递方式）
        console.log('需要明细数据，请使用数据传递方式');
        // loadContractDetails(orderData.purchaseOrderId);
    }

    // 加载订单明细数据
    function loadOrderDetails(orderId) {
        $.xAjax('/purchaseOrder/getOrderDetails/' + orderId, 'GET', '', {
            onSuccess: function(result) {
                if (result.code == 0) {
                    var details = result.data;
                    // 将订单明细数据填充到合同明细表格
                    fillContractDetailsTable(details);
                } else {
                    $.common.showErrorMessage(result.message);
                }
            }
        });
    }

    // 填充合同明细表格
    function fillContractDetailsTable(details) {
        // 重新渲染表格数据
        table.reload('contractDetailTable', {
            data: details
        });
    }

    // 加载合同明细
    function loadContractDetails(orderId) {
        table.reload('contractDetailTable', {
            where: {
                orderId: orderId
            }
        });
    }

    // 保存合同
    function saveContract(data) {
        $.xAjax('/purchaseContract/generateFromOrder', 'POST', JSON.stringify(data), {
            onSuccess: function(result) {
                if (result.code == 0) {
                    $.common.showSuccessMessage('合同生成成功');
                    parent.layer.closeAll();
                    parent.table.reload('currentTableId');
                } else {
                    $.common.showErrorMessage(result.message);
                }
            }
        });
    }

    // 预览合同
    function previewContract() {
        // 实现合同预览功能
        $.common.showInfoMessage('合同预览功能开发中...');
    }

    // 打印合同
    function printContract() {
        // 实现合同打印功能
        window.print();
    }
</script>

</body>
</html>
